﻿Imports Microsoft.VisualStudio.TestTools.UnitTesting

Imports QBuilder
Imports System.Collections.ObjectModel

<TestClass()> _
Public Class JoinTest

    Private testContextInstance As TestContext

    Public Property TestContext() As TestContext
        Get
            Return testContextInstance
        End Get
        Set(value As TestContext)
            testContextInstance = value
        End Set
    End Property

    <TestMethod()>
    Public Sub Join1()
        Dim target As SelectQuery = New SelectQuery()
        target.Fields.Add("Table1.FieldC")
        target.Fields.Add("Table2.FieldD")
        target.TableName = "Table1"
        target.AddCriteria("Table1.FieldE", Operation.EqualTo, "1")
        target.AddJoin("Table2", "Table1.FieldA", Operation.EqualTo, "Table2.FieldB")

        Dim expected As String = "SELECT Table1.FieldC, Table2.FieldD FROM Table1 INNER JOIN Table2 ON Table1.FieldA = Table2.FieldB WHERE Table1.FieldE = 1"
        Dim actual As String
        actual = target.GetSQLText
        Assert.AreEqual(expected, actual)
    End Sub

    <TestMethod()>
    Public Sub Join2()
        Dim target As SelectQuery = New SelectQuery()
        target.Fields.Add("Table1.FieldC")
        target.Fields.Add("Table2.FieldD")
        target.TableName = "Table1"
        target.AddCriteria("Table1.FieldE", Operation.EqualTo, "1")

        Dim join As New JoinInfo("Table2", "Table1.FieldA", Operation.EqualTo, "Table2.FieldB")
        join.JoinType = JoinType.LeftOutter
        target.AddJoin(join)

        Dim expected As String = "SELECT Table1.FieldC, Table2.FieldD FROM Table1 LEFT JOIN Table2 ON Table1.FieldA = Table2.FieldB WHERE Table1.FieldE = 1"
        Dim actual As String
        actual = target.GetSQLText
        Assert.AreEqual(expected, actual)

    End Sub

End Class